﻿using Newtonsoft.Json;
using NWMES;
using NWMES.Mes;
using NWMES.MiAssembleComponentsToSfcsServiceService;
using System;
using System.Net;

namespace MyMes.Mes
{

    /// <summary>
    /// 总成装配
    /// 
    /// http://ndmes7.catlbattery.com:8103/atlmeswebservice/MiAssembleComponentsToSfcsServiceService?wsdl
    /// 
    /// </summary>
    public class MiAssembleComponentsToSfcs : MESBaseService
    {
        static string DefaultApiSection = "MiAssembleComponentsToSfcs";
        public static NetworkCredential networkCredential = null;
        public static MiAssembleComponentsToSfcs Params = null;

        static MiAssembleComponentsToSfcs()
        {
            Params = config[DefaultApiSection].ToObject<MiAssembleComponentsToSfcs>();
            networkCredential = new NetworkCredential(Params.UserName, Params.Password);
        }

        public string amount { get;  set; } = "1";

        /// <summary>
        /// 总成装配
        /// </summary>
        /// <param name="sfc">模组号24为</param>
        /// <param name="AssemblyCode">客户码</param>
        /// <param name="strError"></param>
        /// <returns></returns>
        public static MesResult RequestMes(string sfc, string AssemblyCode, string ApiSection)
        {

            MiAssembleComponentsToSfcsServiceService miAssembleComponentsToSfcsServiceService;
            assembleComponentsToSfcsRequest Request;
            miAssembleComponentsToSfcs miAssembleComponentsToSfcs;
            if (!string.IsNullOrEmpty(ApiSection))
            {
                Params = config[ApiSection].ToObject<MiAssembleComponentsToSfcs>();
                networkCredential = new NetworkCredential(Params.UserName, Params.Password);
            }
            else
            {
                ApiSection = DefaultApiSection;
            }

            string ErrorMessage = Params.ParameterVerification();
            if (!string.IsNullOrEmpty(ErrorMessage))
            {
                return new MesResult(-99, ErrorMessage);
            }
            SetRequestParams(sfc, AssemblyCode, out miAssembleComponentsToSfcsServiceService, out Request, out miAssembleComponentsToSfcs);

            MesResult result = MesResult.InitMesResult(JsonConvert.SerializeObject(miAssembleComponentsToSfcs));
            miAssembleComponentsToSfcsResponse Response = new miAssembleComponentsToSfcsResponse();

            try
            {
                Response = miAssembleComponentsToSfcsServiceService.miAssembleComponentsToSfcs(miAssembleComponentsToSfcs);

                result.EndTime = DateTime.Now;
                var ret = Response.@return;
                result.Message = ret?.message;
                result.Code = ret.code;
                result.Success = result.Code == 0;
                result.ResponeJson = JsonConvert.SerializeObject(Response);

                result.Respone = Response;
                result.ret = ret;
            }
            catch (Exception ex)
            {
                result.Code = -99;
                result.Message = ex.Message;
            }
            SaveMesLog(ApiSection, result);
            return result;
        }

        private static void SetRequestParams(string sfc, string AssemblyCode, out MiAssembleComponentsToSfcsServiceService miAssembleComponentsToSfcsServiceService, out assembleComponentsToSfcsRequest Request, out miAssembleComponentsToSfcs miAssembleComponentsToSfcs)
        {
            miAssembleComponentsToSfcsServiceService = new MiAssembleComponentsToSfcsServiceService();
            miAssembleComponentsToSfcsServiceService.Url = Params.Url;
            miAssembleComponentsToSfcsServiceService.Timeout = Params.Timeout;
            miAssembleComponentsToSfcsServiceService.Credentials = networkCredential;
            miAssembleComponentsToSfcsServiceService.PreAuthenticate = true;

            Request = new assembleComponentsToSfcsRequest();
            Request.site = Params.Site;
            Request.user = Params.User;
            Request.operation = Params.Operation;
            Request.operationRev = Params.OperationRevision;
            Request.activity = Params.ActivityId;
            Request.resource = Params.Resource;
            Request.amount = "1";
            Request.assembleDataArray = new assembleData[1];
            Request.assembleDataArray[0] = new assembleData();
            Request.assembleDataArray[0].sfc = sfc;
            Request.assembleDataArray[0].inventoryArray = new inventoryData[1];
            Request.assembleDataArray[0].inventoryArray[0] = new inventoryData();
            Request.assembleDataArray[0].inventoryArray[0].inventory = AssemblyCode;
            Request.assembleDataArray[0].inventoryArray[0].qty = "1";

            miAssembleComponentsToSfcs = new miAssembleComponentsToSfcs();
            miAssembleComponentsToSfcs.AssembleComponentsToSfcsRequest = Request;
        }
    }
}
